package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.FriendBean;
import cn.innosmart.aq.bean.GroupBean;
import cn.innosmart.aq.view.activity.FriendActivity;

/**
 * @Deprecated
 * Created by David on 2015/8/12.
 */
public class FriendListAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<GroupBean> groups;
    private HashMap<String, ArrayList<FriendBean>> childs;
    private View.OnClickListener onModifyClickListener;
    private AquariumBean aquariumBean;

    public FriendListAdapter(Context context, ArrayList<GroupBean> groups, HashMap<String, ArrayList<FriendBean>> childs) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.groups = groups;
        this.childs = childs;
    }


    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int count = childs.get(groups.get(groupPosition).getGroupId()).size();
        return count;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs.get(groups.get(groupPosition).getGroupId()).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder holder;
        if (convertView == null) {
            holder = new GroupViewHolder();
            convertView = mInflater.inflate(R.layout.expandablelistview_friend_group, null);
            holder.listviewGroupCategory = (TextView) convertView.findViewById(R.id.textGroupName);
            holder.ivGroupArrow = (ImageView) convertView.findViewById(R.id.iv_group_arrow);
            holder.tvNum = (TextView) convertView.findViewById(R.id.tv_num);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        String title="";
        if(groups.get(groupPosition).getGroup().equals("all")){
             title ="所有" ;
        }else{
             title =groups.get(groupPosition).getGroup();
        }
        int num = childs.get(groups.get(groupPosition).getGroupId()).size();
        holder.tvNum.setText(String.valueOf(num));
        holder.listviewGroupCategory.setText(title);
        if (isExpanded) {
            holder.ivGroupArrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.eplv_arrow_down));
        } else {
            holder.ivGroupArrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.boxlist_item_arrow_normal));
        }
        return convertView;
    }


    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String key = groups.get(groupPosition).getGroupId();
        ArrayList<FriendBean> friendBeans = childs.get(key);
        ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = mInflater.inflate(R.layout.expandablelistview_friend_child, null);
            holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.tvNoItem = (TextView) convertView.findViewById(R.id.tv_noitem);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.ivChat = (ImageView) convertView.findViewById(R.id.iv_chat);
            holder.rlChild = (RelativeLayout) convertView.findViewById(R.id.rl_child);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        FriendBean friendBean = friendBeans.get(childPosition);
        holder.rlChild.setTag(friendBean);
        holder.rlChild.setOnClickListener(mItemOnClickListener);
        holder.rlChild.setOnLongClickListener(mItemOnLongClickListener);
        holder.tvTitle.setText(friendBean.getNickName());
        return convertView;
    }

    private View.OnClickListener mItemOnClickListener;

    public void setOnItemClickListener(View.OnClickListener mItemOnClickListener) {
        this.mItemOnClickListener = mItemOnClickListener;
    }
    private View.OnLongClickListener mItemOnLongClickListener;

    public void setOnLongClickListener(View.OnLongClickListener mItemOnLongClickListener) {
        this.mItemOnLongClickListener = mItemOnLongClickListener;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    static class GroupViewHolder {
        TextView tvNum;
        ImageView ivGroupArrow;
        TextView listviewGroupCategory;
    }

    static class ChildViewHolder {
        ImageView ivIcon;
        TextView tvTitle;
        ImageView ivChat;
        TextView tvNoItem;
        RelativeLayout rlChild;
    }

    public void refreshData() {
        childs = FriendActivity.childs;
        groups = FriendActivity.groups;
        notifyDataSetChanged();
    }
}
